//
//  CHSRuntimeViewController.m
//  ZXCategories_Example
//
//  Created by zhuxm2023 on 2024/8/1.
//  Copyright © 2024 海参. All rights reserved.
//

#import "CHSRuntimeViewController.h"
#import <objc/runtime.h>

@interface CHSRuntimeViewController ()
@property (nonatomic, copy) NSArray *array;

@end

@implementation CHSRuntimeViewController

+ (void)load{
//    zxSwizzling_exchangeMethod(self.class, @selector(viewWillAppear:), @selector(viewWillAppear2));
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.navTitle;

    [self testImageMethodList];
    
    [self testMetaClass];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewWillAppear2{
    
}



- (void)associatedObject{
    NSArray *array =@[@"one",@"two"];
    NSString *overview = [[NSString alloc] initWithFormat:@"%@",@"我是"];
    //以key:overviewKey关键字给array创建一个关联的对象overView;retain持有;
    objc_setAssociatedObject(array, @"key", overview, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    NSLog(@"%@",array);
    
    NSString *associatedObject = objc_getAssociatedObject(array, @"key");
    NSLog(@"%@",associatedObject);
}

- (void)testMetaClass{
    //实例对象
    UIControl *control = [UIControl new];
    //类对象-UIControl
    Class cls = object_getClass(control);
    //元类-UIControl-0x00007fff866ae858
    Class meta = object_getClass(cls);
    //元类的类是根类元类-NSObject-(Class)0x00007fff864bb6c0
    Class meta_root = object_getClass(meta);
    
    //根类实例对象-(NSObject *) 0x0000600001104390
    NSObject *obj = [NSObject new];
    //根类类对象-(Class)NSObject
    Class objCls = object_getClass(obj);
    //根类元类-NSObject-(Class)0x00007fff864bb6c0
    Class objMeta = object_getClass(objCls);
    //根类元类的类依然是根类元类-NSObject-(Class)0x00007fff864bb6c0
    Class objMeta_root = object_getClass(objMeta);
}

- (void)testGetClass{
    //CHSViewController
    Class cls1 = object_getClass(self);
    //CHSViewController
    Class meta1 = objc_getMetaClass(class_getName(cls1));

    //UIControl
    Class cls2 = [UIControl class];
    //UIControl
    Class meta2 = object_getClass(cls2);
    BOOL isMeta2 = class_isMetaClass(meta2);
    //UIControl
    Class meta3 = objc_getMetaClass(class_getName(cls2));

    BOOL isClass =  object_isClass(meta3);

}

- (void)testImageMethodList{
    const char *librarieName = class_getImageName([self class]);
    unsigned int outCount;
    const char **librarieAllClass = objc_copyClassNamesForImage(librarieName, &outCount);
    NSLog(@"%u",outCount);
    for (int i = 0; i < outCount; i++)
    {
        const char *name = librarieAllClass[i];
        Class cls = objc_getClass(name);
//        [cls getClassMethodListWithLog:YES block:^(SEL sel) {
//            if (sel == @selector(load)) {
//                NSLog(@"class name: %s", name);

//            }
//        }];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
